package com.bpzj.mybatis;

import com.bpzj.mybatis.mapper.BlogMapper;
import com.bpzj.mybatis.model.Blog;
import com.mysql.cj.jdbc.MysqlDataSource;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@MapperScan("com.bpzj.mybatis.mapper")
public class MybatisWithSpring {

  public static void main(String[] args) {
    ApplicationContext context = new AnnotationConfigApplicationContext(MybatisWithSpring.class);
    BlogMapper blogMapper = context.getBean(BlogMapper.class);
    List<Blog> blogs = blogMapper.selectAll();
    blogMapper.selectAll();
  }

  @Bean
  public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    MysqlDataSource datasource = new MysqlDataSource();
    datasource.setUrl("jdbc:mysql://192.168.137.10:3306/test");
    datasource.setUser("root");
    datasource.setPassword("minmin");
    factoryBean.setDataSource(datasource);
    // 配置
    factoryBean.setMapperLocations();
    // TODO 检查这里能否正常执行
    return factoryBean.getObject();
  }

}
